<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>有奖答题平台</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
    <link rel="stylesheet" href="/lib/mobile/css/common.css"/>
    <link rel="stylesheet" href="/lib/mobile/css/login.css"/>
</head>
<body>
    <div class="login">
        <div class="login_form">
            <form action="">
                <dl>
                    <dt>手机号</dt>
                    <dd><input type="number" id="phone" placeholder="请填写您的手机号" /></dd>
                </dl>
                <dl>
                    <dt>验证码</dt>
                    <dd><input type="number" id="code" placeholder="请输入验证码" /><button id="btnSend">获取验证码</button></dd>
                </dl>
                <p><i></i><span id="doText">请输入手机号，获取验证码登录答题！</span></p>
            </form>
            <div class="login_button" id="login"><a href="javascript: void(0);">提交</a></div>
           <!-- <div class="login_button on"><a href="javascript: void(0);">提交</a></div> -->
        </div>
        <div class="login_close"> </div>
    </div>
    <!-- 依赖库 手机调试的js引用顺序如下 -->
    <script src="/lib/mobile/js/zepto.js"></script>
    <script src="/lib/mobile/js/plugins/fastclick.js"></script>
    <script src="/lib/mobile/js/bui.js"></script>
    <script src="/lib/mobile/js/common.js?1111    "></script>
    <script>
        bui.ready(function () {
            // 这里写业务及控件初始化
            // initCommon(bui,"0");


            bui.btn({id: ".bui-page", handle: ".bui-btn"}).load();
            var pageview = {};
            pageview.init = function () {
                // 验证码示例
                var $btnSend = $("#btnSend");
                var $phone = $("#phone");
                $("#phone").blur(function(){
                    var loginAjax = bui.ajax({
                        url: BASE_URL+"front/checkPhone",
                        // 可选参数
                        data: {
                            phone:$(this).val()
                        },
                        method: "POST"
                    });
                    loginAjax.then(function(res) {
                        $("#doText").html(res.msg);
                    },function(res) {
                        bui.alert("请求异常");
                    })
                });
                var timer = bui.timer({
                    onEnd: function () {
                        $btnSend.removeClass("disabled").text("重新获取验证码");
                    },
                    onProcess: function (e) {
                        var valWithZero = e.count < 60 ? "0" + e.count : e.count;
                        $btnSend.text(valWithZero + "后重新获取");
                    },
                    times: 60
                });

                $("#login").on("click", function (argument) {
                    var phone = $("#phone").val();
                    if($.trim(phone) == ""){
                        bui.alert("手机号不能为空");
                        return ;
                    }
                    var code = $("#code").val();
                    if($.trim(code) == ""){
                        bui.alert("验证码为空");
                        return ;
                    }
                    //执行登录操作
                    var loginAjax = bui.ajax({
                        url: BASE_URL+"front/login",
                        // 可选参数
                        data: {
                            phone:phone,
                            code:code
                        },
                        method: "POST"
                    });

                    loginAjax.then(function(res) {
                        console.log(res);
                        if(res.code == 0){
                            var user = res.data;
                            console.log(user);
                            if(user){
                                var storage = bui.storage();
                                var token = user.token;
                                storage.set("yhbm",user.xm);
                                storage.set("phone",user.phone);
                                storage.set("user",user);
                                storage.set("membercode",user.membercode);
                                storage.set("uid",user.id);
                                storage.set("token",token);
                                $("#doText").html("登录成功");
                                TOKEN = token;
                                bui.config.ajax = {
                                    headers: {
                                        token:token
                                    }
                                };
                                bui.load({ url: BASE_URL+"mobile/index"});
                            }
                        }else{
                            $("#doText").html(res.msg);
                        }
                    },function(res) {
                        bui.alert("请求异常");
                    })
                });
                $btnSend.on("click", function (argument) {
                    var phone = $("#phone").val();
                    if($.trim(phone) == ""){
                        $("#doText").html("手机号不能为空");
                        return ;
                    }
                    var hasDisabled = $(this).hasClass("disabled");
                    if (!hasDisabled) {
                        var data = {
                            phone:phone
                        }
                        var codeAjax = bui.ajax({
                            url: BASE_URL+"/front/sendValidCode",
                            // 可选参数
                            data:data,
                            method: "GET"
                        });
                        codeAjax.then(function(res) {
                            console.log(res,"success")
                            if(res.code == 0){
                                $(this).addClass("disabled")
                                bui.hint("验证码发送成功")
                                timer.restart();
                            }else{
                                bui.alert(res.msg);
                            }
                        },function(res) {
                            bui.alert("请求异常");
                        })
                    }
                })

            }
            pageview.init();
        })
    </script>
</body>

</html>